package suo;

public class Runna implements Runnable{
    private int flag=1;
    static Object resource1=new Object(),resource2 = new Object();

    @Override
    public void run() {
        if (flag == 1) {
            synchronized (resource1){
                System.out.println("resource1");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (resource2){
                    System.out.println("resource2");
                }
            }
        }
        if (flag == 0) {
            synchronized (resource2){
                System.out.println("resource2");
                try {
                    Thread.sleep(1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                synchronized (resource1){
                    System.out.println("resource1");
                }
            }

        }

    }

    public static void main(String[] args) {
        Runna lock1=new Runna();
        Runna lock2=new Runna();
        lock1.flag=1;
        lock2.flag=0;
        Thread ll1=new Thread(lock1);
        Thread ll2=new Thread(lock2);
        ll1.start();
        ll2.start();
    }
}
